package bgi.service;  

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.data.mongodb.core.MongoTemplate;  
import org.springframework.data.mongodb.core.query.Criteria;  
import org.springframework.data.mongodb.core.query.Query;  
import org.springframework.stereotype.Service;  
  
import bgi.pojo.User;  
  
  
@Service   
public class UserService {  
      
    private static String USER_COLLECTION = "user";  
  
    @Autowired  //自动绑定
    MongoTemplate mongoTemplate;  
      
    /** 
     *  
     * @param user 
     */  
    public void saveUser(User user){  
          
        mongoTemplate.save(user, USER_COLLECTION);  
          
    }  
      
    /** 
     *  
     * @param name 
     * @return  
     */  
    public User findUserByName(String name){  
          
        return mongoTemplate.findOne(new Query(Criteria.where("name").is(name)), User.class, USER_COLLECTION);  
         
    }  
    
    /** 
     * @category test
     * @author yuanye
     * @param age 
     */ 
    public User findUserByAge(int age){  
        
        return mongoTemplate.findOne(new Query(Criteria.where("age").is(age)),User.class,USER_COLLECTION);
    
    }  
    
}